package com.china.springServer.config;

import lombok.Data;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @fileName: ZookeeperConfig
 * @author: LmR
 * @createDate: 2023/10/21 20:27
 * @Desc:
 */
@Configuration
@ConfigurationProperties(prefix = "zookeeper.curator")
@Data
public class ZookeeperConfig {

    private String address;

    private Integer connectionTimeoutMs;

    private Integer sessionTimeOut;

    private Integer maxRetries;

    private Integer sleepMsBetweenRetry;

    private String namespace;

    @Bean("curatorClient")
    public CuratorFramework curatorFramework() {
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(address)
                .connectionTimeoutMs(connectionTimeoutMs)
                .sessionTimeoutMs(sessionTimeOut)
                .retryPolicy(new ExponentialBackoffRetry(sleepMsBetweenRetry, maxRetries))
                .namespace(namespace)       // 命名空间(操作节点时会以命名空间作为父节点，公共区域)
                .build();
        client.start();
        return client;
    }
}
